package com.hj.sort;

import java.util.logging.Logger;

/**
 * @author hj
 * @Date 2021/12/17
 * @Description: 冒泡排序
 */
public class BubbleSort {

    Logger logger = Logger.getLogger("bubbleSort");

    /**
     * 冒泡排序
     *
     * @param array 整型数组
     * @return 排序后的数组
     */
    public int[] bubbleSort(int[] array) {
        if (null == array) {
            logger.info("传入的数组是空值");
            return null;
        }
        int temp;
        for (int i = array.length - 1; i > 0; i--) {
            for (int j = 0; j < i; j++) {
                // 将大值交换到后面
                if (array[j] > array[j + 1]) {
                    temp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = temp;
                }
            }
        }
        return array;
    }

    public static void main(String[] args) {
        BubbleSort bubbleSort = new BubbleSort();
        int[] array = {5, 9, 85, 45, 25, 36, 98, 45, 12, 35};
        array = bubbleSort.bubbleSort(array);
        for (int a : array) {
            System.out.println(a);
        }
    }

}
